package com.laozhao.wintersystem;

import java.awt.Font;
import java.awt.Image;
import java.awt.SystemTray;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import twaver.TWaverConst;
import twaver.TWaverUtil;

import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.plastic.PlasticTheme;

import free.FreePopupMenuBorder;
import free.FreeTabbedPaneUI;
import free.FreeUtil;

public class WSystemModel {

//	private WActions actions = new WActions();
//	private WListeners listeners = new listeners;
	
	private Object splash;
	private Map<String, WFrameModel> frames = new HashMap<String, WFrameModel>();
	private SystemTray tray;
	
	@SuppressWarnings("unused")
	private static final String ELEMENT_SYSTEM = "system"; 
	private static final String ELEMENT_SYSTEM_SPLASH = "splash"; 
	private static final String ELEMENT_SYSTEM_FRAME = "frame"; 
	private static final String ELEMENT_SYSTEM_TRAY = "tray"; 
	
	@SuppressWarnings("unused")
	private static final String ATTLIST_SYSTEM_NAME = "name"; 
	private static final String ATTLIST_SYSTEM_LOOK = "look"; 
	
//	private static final String ATTLIST_SPLASH_CLASS = "class"; 
//	private static final String ATTLIST_SPLASH_NAME = "name"; 
//	private static final String ATTLIST_SPLASH_IMG = "icon"; 
//	private static final String ATTLIST_SPLASH_TIP = "tip";
//
//	private static final String ATTLIST_TRAY_CLASS = "tray"; 
//	private static final String ATTLIST_TRAY_NAME = "name"; 
//	private static final String ATTLIST_TRAY_ICON = "icon"; 
//	private static final String ATTLIST_TRAY_TIP = "tip";
	
	String tmpLook;
	String tmpClass ;
	String tmpName ;
	Icon tmpIcon;;
	Image tmpImg;
	String tmpTip;
	
	public WSystemModel(){
		
	}
	
	public void loaSystemModel(String xmlPath){
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(getClass().getResource(xmlPath)
					.openStream());
//			Document doc = db.parse(xmlFile);

			Element root = doc.getDocumentElement();
			loadSystemModel(root);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public WSystemModel loadSystemModel(Element root){
		if (root != null) {			
			tmpLook = WSTools.getStringAttribute(root, ATTLIST_SYSTEM_LOOK);
			if (tmpLook != null && !"".equals(tmpLook)) {
				setupLookAndFeel(tmpLook);
			}
			NodeList nodes = root.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) {
				org.w3c.dom.Node node = nodes.item(i);
				if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
					if (node.getNodeName().equalsIgnoreCase(ELEMENT_SYSTEM_SPLASH)) {
						loadSplashModel(node);
					}else if (node.getNodeName().equalsIgnoreCase(ELEMENT_SYSTEM_FRAME)) {
						loadFrameModel(node);
					}else if (node.getNodeName().equalsIgnoreCase(ELEMENT_SYSTEM_TRAY)) {
						loadTrayModel(node);
					}
				}
			}
		}
		
		return null;
	}
	
	private void loadFrameModel(Node node) {
		WFrameModel frameModel = new WFrameModel(node);
	}
	
	private void loadSplashModel(Node nodes){
		// TODO
	}
	

	private void loadTrayModel(Node nodes){
		// TODO
	}
	
	private void setupLookAndFeel(String looks) {
        Locale.setDefault(TWaverConst.EN_US);
        TWaverUtil.setLocale(TWaverConst.EN_US);
        PlasticTheme theme = new com.jgoodies.looks.plastic.theme.ExperienceGreen() {

            @Override
            public FontUIResource getControlTextFont() {
                return new FontUIResource(new Font("Calibri", Font.PLAIN, 11));
            }
        };
        PlasticLookAndFeel.setPlasticTheme(theme);
        try {
            UIManager.setLookAndFeel(looks);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        
        //use my tab ui.
        UIManager.getDefaults().put("TabbedPaneUI", FreeTabbedPaneUI.class.getName());
        UIManager.put("Menu.selectionBackground", FreeUtil.NO_COLOR);
        UIManager.put("MenuItem.selectionBackground", FreeUtil.MENUITEM_SELECTED_BACKGROUND);
        UIManager.put("PopupMenu.border", new FreePopupMenuBorder());
        UIManager.put("ToolTip.font", FreeUtil.FONT_14_BOLD);
        UIManager.put("TabbedPane.contentBorderInsets", FreeUtil.ZERO_INSETS);
        UIManager.put("TabbedPane.tabInsets", FreeUtil.ZERO_INSETS);
        UIManager.put("TabbedPane.selectedTabPadInsets", FreeUtil.ZERO_INSETS);
        UIManager.put("TabbedPane.tabAreaInsets", FreeUtil.ZERO_INSETS);
    }
}
